package com.grantz.function.bbs.core.controller;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.grantz.function.bbs.core.entity.GroupDetail;
import com.grantz.function.bbs.core.inter.GroupDetailService;
import com.grantz.model.bbs.GroupDetailModel;
import com.grantz.model.bbs.GroupDetailSearch;
import com.grantz.models.base.element.page.ElementPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文件名称： com.grantz.function.bbs.core.controller.GroupDetailController.java</br>
 * 初始作者： WenBin</br>
 * 创建日期： 2020年2月13日</br>
 * 功能说明： 群组明细<br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者        日期       修改内容<br/>
 * ================================================<br/>
 *  Copyright (c) 2010-2011 .All rights reserved.<br/>
 */
@RestController
@RequestMapping("/api/v1/groupdetails")
@Api(value="群组明细记录",tags={"群组明细记录接口"})
public class GroupDetailController {
	
	@Resource
	private GroupDetailService  groupDetailService;

	// 获取群组用户
	@ApiOperation(value = "获取分页信息")
	@GetMapping
	public ResponseEntity<ElementPage<GroupDetailModel>>  get( GroupDetailSearch query) throws Exception{
		ElementPage<GroupDetailModel> element =  groupDetailService.queryElementPage(query);
		return new ResponseEntity<ElementPage<GroupDetailModel>>(element ,HttpStatus.CREATED);
	}

	// 添加信息
	@ApiOperation(value = "添加群组详细信息")
	@PutMapping
	public ResponseEntity<?>  add( GroupDetailModel  model) throws Exception{
		//  添加信息
		GroupDetail  entity = pop(new  GroupDetail(), model);
		groupDetailService.save(entity);
		return new ResponseEntity<>(HttpStatus.CREATED);
	}

	// 添加信息
	@ApiOperation(value = "更新群组详细信息")
	@PostMapping
	public ResponseEntity<?>  update( GroupDetailModel  model) throws Exception{
		Optional<GroupDetail>   option = groupDetailService.findOneById(model.getId());
		//  添加信息
		if(option.isPresent()) {
			GroupDetail  entity = pop(option.get(), model);
			groupDetailService.save(entity);
		}
		return new ResponseEntity<>(HttpStatus.CREATED);
	}

	private  GroupDetail   pop(GroupDetail entity ,GroupDetailModel  model) {
		entity.setId(entity.getId());
		return entity;
	}

}
